package com.googlecode.intellimars.winstone;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.util.DefaultJDOMExternalizer;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.ui.HyperlinkLabel;
import com.intellij.ide.BrowserUtil;
import org.jdom.Element;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.HyperlinkEvent;
import java.io.File;
import java.util.ResourceBundle;

public class WinstoneRuntimeConfiguration implements ApplicationComponent, Configurable, JDOMExternalizable {
    @NonNls
    private static ResourceBundle ourBundle = ResourceBundle.getBundle("com.googlecode.intellimars.winstone.message");
    private static final Icon ICON = IconLoader.getIcon("/com/googlecode/intellimars/winstone/large-icon.gif");
    private static String DEFAULT_COMMONLIB_DIRECTORY;

    static {
        File commonLib = new File(new File(PathManager.getSystemPath(), "plugins"), "winstone-plugin");
        commonLib.mkdir();
        commonLib = new File(commonLib, "commonLibs");
        commonLib.mkdir();
        DEFAULT_COMMONLIB_DIRECTORY = commonLib.getAbsolutePath();
    }

    private JPanel panel;
    private TextFieldWithBrowseButton winstoneJar;
    private TextFieldWithBrowseButton commonLib;
    private HyperlinkLabel hyperlinkLabel;

    public String WINSTONE_JAR = "";
    public String COMMONLIB_DIRECTORY = DEFAULT_COMMONLIB_DIRECTORY;


    public static WinstoneRuntimeConfiguration getInstance() {
        return ApplicationManager.getApplication().getComponent(WinstoneRuntimeConfiguration.class);
    }

    public WinstoneRuntimeConfiguration() {
        winstoneJar.addBrowseFolderListener("Winstone", ourBundle.getString("select.winstone.jar"), null,
                                            new FileChooserDescriptor(true, false, true, true, false, false));
        commonLib.addBrowseFolderListener("Winstone", ourBundle.getString("select.commonLibs"), null,
                                          new FileChooserDescriptor(false, true, false, false, false, false));
    }

    public void initComponent() { }

    public void disposeComponent() { }

    @NotNull
    public String getComponentName() {
        return "WinstoneRuntimeConfiguration";
    }

    @Nls
    public String getDisplayName() {
        return "Winstone";
    }

    @Nullable
    public Icon getIcon() {
        return ICON;
    }

    @Nullable
    @NonNls
    public String getHelpTopic() {
        return null;
    }

    public JComponent createComponent() {
        return panel;
    }

    public boolean isModified() {
        return !WINSTONE_JAR.equals(winstoneJar.getText())
               || !COMMONLIB_DIRECTORY.equals(commonLib.getText());
    }

    public void apply() throws ConfigurationException {
        WINSTONE_JAR = winstoneJar.getText();
        COMMONLIB_DIRECTORY = commonLib.getText();
    }

    public void reset() {
        winstoneJar.setText(WINSTONE_JAR);
        commonLib.setText(COMMONLIB_DIRECTORY);
    }

    public void disposeUIResources() { }


    public void readExternal(Element element) throws InvalidDataException {
        DefaultJDOMExternalizer.readExternal(this, element);
    }

    public void writeExternal(Element element) throws WriteExternalException {
        DefaultJDOMExternalizer.writeExternal(this, element);
    }

    private void createUIComponents() {
        hyperlinkLabel = new HyperlinkLabel("Winstone Site");
        hyperlinkLabel.addHyperlinkListener(new HyperlinkListener() {
            public void hyperlinkUpdate(HyperlinkEvent e) {
                BrowserUtil.launchBrowser("http://winstone.sourceforge.net");
            }
        });
    }
}
